﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_SalaryCommand : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;
        //private Command_BLL salaryCommandBLL = new Command_BLL();
        private ICommandBLL cmdBLL = new CommandBLL();
        private IMasRecordStatusBLL staComBLL = new MasRecordStatusBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private IJobSiteLevelSystemControlBLL jslscBLL = new JobSiteLevelSystemControlBLL();
        private IJobSiteTypeBLL jobTypeBLL = new JobSiteTypeBLL();
        private EmployeeInfo empInfo;
        private PositionInfo posInfo;
        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindStaCom();
                bindEmpType();
                FillDataToApprover();

                DataSet ds = new DataSet();
                ds = jslscBLL.ListJobSiteLevelSystemControl();
                bool bMis = ds.Tables[0].Rows[0][0].ToString() == "1";

                rdoFlag.Items[0].Enabled = !bMis;
                rdoFlag.Items[1].Selected = bMis;
                rdoApptypeOut.Enabled = !bMis;
                txtAppName.Enabled = !bMis;
                rdoFlag.Items[0].Selected = !bMis;               

                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    ddlStatus.SelectedValue = DataBinder.Eval(DataItem, "Status").ToString();
                    string strFlag = DataBinder.Eval(DataItem, "CmdFlag").ToString();
                    if (strFlag == "1")
                        rdoFlag.Items[0].Selected = true;
                    if (strFlag == "2")
                        rdoFlag.Items[1].Selected = true;
                    if (strFlag == "3")
                        rdoFlag.Items[2].Selected = true;

                    txtNo.Text = DataBinder.Eval(DataItem, "DraftNo").ToString();
                    txtRefNo.Text = DataBinder.Eval(DataItem, "RefNo").ToString();
                    txtName.Text = DataBinder.Eval(DataItem, "Descriptions").ToString();


                    ddlEmployeeType.SelectedValue = DataBinder.Eval(DataItem, "EmployeeType.Sequence").ToString();
                    txtDateApproved.SetValue(DataBinder.Eval(DataItem, "ApprovedDate", "{0:dd/MM/yyyy}").ToString());

                    rdoApptypeIn.Checked = DataBinder.Eval(DataItem, "ApprovedBy") != null;
                    rdoApptypeOut.Checked = !rdoApptypeIn.Checked;

                    txtAppName.Text = DataBinder.Eval(DataItem, "ApprovedName").ToString();

                    if (DataBinder.Eval(DataItem, "ApprovedBy") != null)
                    {
                        cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "ApprovedBy").ToString();
                        txtAppName.Text = "";
                    }

                    cmbEmployee.Enabled = rdoApptypeIn.Checked;
                    txtAppName.Enabled = rdoApptypeOut.Checked;
                    txtAppPosition.Text = DataBinder.Eval(DataItem, "ApprovedPositions").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                }
                else
                {
                    ddlEmployeeType.SelectedValue = Session["EMPTYPE"].ToString();
                    //cmbComType.SelectedValue = Session["CMDTYPE"].ToString();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void bindStaCom()
        {
            IList<MasRecordStatusInfo> ListStatusCommand = staComBLL.ListStatusCommand();
            ddlStatus.DataSource = ListStatusCommand;
            ddlStatus.DataBind();
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
            ddlEmployeeType.DataSource = infoList;
            ddlEmployeeType.DataBind();
        }

        public void FillDataToApprover()
        {
            DataSet ds = empBLL.LoadEmployeeForApproveCommands();
            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dt.Rows[i]["EMP_NAME"].ToString();
                item.Value = dt.Rows[i]["EMP_SEQ"].ToString();

                item.Attributes.Add("EMP_NAME", dt.Rows[i]["EMP_NAME"].ToString());
                item.Attributes.Add("EXEC_POS_NAME", dt.Rows[i]["EXEC_POS_NAME"].ToString());

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }
        public void CheckValidate()
        {
            reqDraftNo.Enabled = ddlStatus.SelectedValue == "W";
            reqRefNo.Enabled = ddlStatus.SelectedValue == "A";
            reqEmployee.Enabled = rdoApptypeIn.Checked;
            reqAppName.Enabled = rdoApptypeOut.Checked;
        }
 
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                CheckValidate();

                cmdBLL.InsertCommandEmployee(SystemInfo._CmdEmployee
                                             , rdoFlag.SelectedValue
                                             , txtNo.Text.Trim()
                                             , txtRefNo.Text.Trim()
                                             , txtName.Text.Trim()
                                             , ""
                                             , ""
                                             , ddlStatus.SelectedValue
                                             , Int32.Parse(ddlEmployeeType.SelectedValue)
                                             , Int32.Parse(Session["CMDTYPE"].ToString())
                                             ,
                                             cmbEmployee.SelectedValue.Length > 0
                                                 ? int.Parse(cmbEmployee.SelectedValue)
                                                 : (int?) null
                                             , txtAppName.Text.Trim()
                                             , txtAppPosition.Text.Trim()
                                             ,
                                             txtDateApproved.GetStringValue().Length > 0
                                                 ? DateTime.Parse(txtDateApproved.GetStringValue())
                                                 : (DateTime?) null
                                             , txtRemarks.Text.Trim()
                                             , ssMng.EmpSeqI);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CheckValidate();

                cmdBLL.UpdateCommandEmployee(SystemInfo._CmdEmployee
                   , Convert.ToInt32(lblID.Text.Trim())
                   , rdoFlag.SelectedValue
                   , txtNo.Text.Trim()
                   , txtRefNo.Text.Trim()
                   , txtName.Text.Trim()
                   , ""
                   , ""
                   , ddlStatus.SelectedValue
                   , Int32.Parse(ddlEmployeeType.SelectedValue)
                   , Int32.Parse(Session["CMDTYPE"].ToString())
                   , cmbEmployee.SelectedValue.Length > 0 ? int.Parse(cmbEmployee.SelectedValue) : (int?)null
                   , txtAppName.Text.Trim()
                   , txtAppPosition.Text.Trim()
                   , txtDateApproved.GetStringValue().Length > 0 ? DateTime.Parse(txtDateApproved.GetStringValue()) : (DateTime?)null
                   , txtRemarks.Text.Trim()
                   , ssMng.EmpSeqI);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = false;
            cmbEmployee.Enabled = true;
            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            txtAppName.Text = "";
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = true;
            cmbEmployee.Enabled = false;

            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            cmbEmployee.Text = "... เลือกผู้อนุมัติ...";
        }
               

        protected void cmbEmployee_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //txtAppPosition.Text = 
            //empBLL.ListEmployees(cmbEmployee.SelectedValue);
            //posBLL.ListPositionsBySequence(

            IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
            IList<PositionInfo> posList;
            if (empList.Count > 0)
            {
                empInfo = empList[0];
                posList = posBLL.ListPositionsBySequence(empInfo.Position.Sequence.ToString());
                posInfo = posList[0];
                //  Label6.Text = empInfo.Address.Phone;
                txtAppPosition.Text = posInfo.Executive.Name;
            }

        }

    }
}